<?php $this->load->view('headerfooter/header'); ?>
	<link rel="stylesheet" href="<?=base_url()?>/src/css/jqueryui.css">
	<script src="<?=base_url()?>/src/js/jqueryui-1.10.4.min.js"></script>
	<script src="<?=base_url()?>/src/js/parsley.js"></script>
	<script type="text/javascript">
		$(function() {
			$("#datepicker").datepicker();
			$("#datepicker1").datepicker();
			$("#datepicker").datepicker("option","dateFormat","yy-mm-dd");
			$("#datepicker1").datepicker("option","dateFormat","yy-mm-dd");
		});
	</script>
        <?php
            $url = 'surat/insert';
            $idsurat = '';
            $nama_instansi = '';
            $tanggal_kerjasama = '';
            $masa_berlaku = '';
            $kerjasama_mengenai = '';
            $kode_surat = '';
            $deskripsi = '';
            $unit_terkait = '';
            $implementasi = '';
            $scan_file = '';
            $kategori = '';
            $subkategori = '';
            $iduser = '';
			$nomer_surat='';
			$message = $this->session->flashdata('message');
			
            if (isset($value)) {
                $url = 'surat/update';
                $idsurat = $value['idsurat'];
                $nama_instansi = $value['nama_instansi'];
                $tanggal_kerjasama = $value['tanggal_kerjasama'];
                $masa_berlaku = $value['masa_berlaku'];
                $kerjasama_mengenai = $value['kerjasama_mengenai'];
                $kode_surat = $value['kode_surat'];
                $deskripsi = $value['deskripsi'];
                $unit_terkait = $value['unit_terkait'];
                $implementasi = $value['implementasi'];
                $scan_file = $value['scan_file'];
                $kategori = $value['kategori'];
                $iduser = $value['iduser'];
				$nomer_surat=$value['nomer_surat'];
            }
        ?>
		<script>
			$(function() {
				var $datepicker = $( "#datepicker" );
				$datepicker.datepicker();
				$datepicker.datepicker('setDate', '<?=$tanggal_kerjasama?>');
				
				$datepicker = $("#datepicker1");
				$datepicker.datepicker();
				$datepicker.datepicker('setDate', '<?=$masa_berlaku?>');
			});
		</script>
        <div class="container">
            <div class="col-sm-4 col-sm-offset-1">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <div class="panel-title" style="text-align:center"><?=$title?> <?php if($kode_surat != ''){echo '&mdash; '.$kode_surat;} ?></div>
                    </div>
                    <div class="panel-body">
						<?php if($message != null):?>
						<div class="alert alert-warning">
							<button type="button" class="close" data-dismiss="alert">&times;</button>
							<?=$message?>
						</div>
						<?php endif; ?>
                        <div class="row">
                        <?= form_open_multipart($url, 'data-parsley-validate'); ?>            
							<h3 class="text-center"><strong>Kategori</strong></h3>
							<div class="form-group form-inline form-insert-kategori">
								<?php // apabila halaman edit
									if(isset($value)):?>
									<select id="area" class="combobox" onchange="ShowParentKategori(this.value)">
										<option value="-1">Pilih</option>
										<?php foreach($kategori_parent as $data_kategori):?>
											<option value="<?=$data_kategori['id']?>"
												<?php if($data_kategori['id'] == $kategori_selected[2]["id"]):?>
												 selected
												<?php endif;?>>
												<?=$data_kategori['nama']?>
											</option>
										<?php endforeach; ?>
									</select> -
									<select id="dinas" class="combobox" onchange="showKategori(this.value)">
										<option value="">Pilih</option>
										<?php foreach($subkategori_parent as $data_kategori):?>
											<option value="<?=$data_kategori['id']?>"
												<<?php if($data_kategori['id'] == $kategori_selected[1]["id"]):?>
												 selected
												<?php endif;?>>
												<?=$data_kategori['nama']?>
											</option>
										<?php endforeach; ?>
									</select> -
									<select name="kategori" class="combobox" id="kategori" required>
										<option value="">Pilih</option>
										<?php foreach($childkategori as $data_kategori):?>
											<option value="<?=$data_kategori['id']?>"
												<?php if($data_kategori['id'] == $kategori_selected[0]["id"]):?>
												 selected
												<?php endif;?>>
												<?=$data_kategori['nama']?>
											</option>
										<?php endforeach; ?>
									</select>
								<?php // apabila halaman insert
									else:?>
									<select id="area" class="combobox" onchange="ShowParentKategori(this.value)">
										<option value="-1">Pilih</option>
										<?php foreach($kategori_parent as $data_kategori):?>
											<option value="<?=$data_kategori['id']?>"><?=$data_kategori['nama']?></option>
										<?php endforeach; ?>
									</select> -
									<select id="dinas" class="combobox" onchange="showKategori(this.value)">
										<option value="">Pilih</option>
										<!-- option value="-1">Pilih</option><option value="6">Internasional</option><option value="11">Nasional</option -->
									</select> -
									<select name="kategori" class="combobox" id="kategori" required>
										<option value="">Pilih</option>
									</select>
								<?php endif;?>
							</div>
							<input name="id" type="hidden" value="<?= $idsurat ?>"/>
							<div class="form-group">
                                <label for="KodeSurat" class="col-sm-2 control-label">Kode Surat</label>
                                <div class="col-sm-10">
                                    <input name="kode-surat" class="form-control" type="text" placeholder="Kode Surat" value="<?= $kode_surat ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="Instansi" class="col-sm-2 control-label">Nama Instansi</label>
                                <div class="col-sm-10">
                                    <input name="nama-instansi" class="form-control" type="text" placeholder="Nama Instansi" value="<?= $nama_instansi ?>" required/><br>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="TglKerjasama" class="col-sm-2 control-label">Tanggal Kerjasama</label>
                                <div class="col-sm-10">
                                    <input id ="datepicker" name="tanggal-kerjasama" class="form-control" type="text" placeholder="Tanggal Kerjasama" value="<?= $tanggal_kerjasama ?>" required/><br>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="TanggalKadaluarsa" class="col-sm-2 control-label">Tanggal Kadaluarsa</label>
                                <div class="col-sm-10">
                                    <input id ="datepicker1" name="masa-berlaku" class="form-control" type="text" placeholder="Masa Berlaku" value="<?= $masa_berlaku ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="Kerjasama" class="col-sm-2 control-label">Kerjasama</label>
                                <div class="col-sm-10">
                                    <input name="kerjasama" class="form-control" type="text" placeholder="Kerjasama" value="<?= $kerjasama_mengenai ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="KodeSurat" class="col-sm-2 control-label">Nomer Surat</label>
                                <div class="col-sm-10">
                                    <input name="nomer-surat" class="form-control" type="text" placeholder="Nomer Surat" value="<?= $nomer_surat ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="Deskripsi" class="col-sm-2 control-label">Deskripsi</label>
                                <div class="col-sm-10">
                                    <textarea name="deskripsi" class="form-control" type="text" placeholder="Deskripsi" required><?= $deskripsi ?></textarea><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="UnitTerkait" class="col-sm-2 control-label">Unit Terkait</label>
                                <div class="col-sm-10">
                                    <input name="unit-terkait" class="form-control" type="text" placeholder="Unit Terkait" value="<?= $unit_terkait ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="Implementasi" class="col-sm-2 control-label">Implementasi</label>
                                <div class="col-sm-10">
                                    <input name="implementasi" type="text" class="form-control" placeholder="Implementasi" value="<?= $implementasi ?>" required/><br>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">Pilih File</label>
                                <div class="col-sm-10">
									<?php // jika sudah ada file
									if($scan_file!=''):?>
										<div class="file-box" id="file-box">
											<i class="fa fa-file-text"></i> <a href="<?=site_url('surat/download/'.$scan_file)?>"><?=$scan_file?></a> 
											<a class="btn" id="delete-file"><i class="fa fa-trash-o"></i> Delete</a>
										</div>
										<script>
										$(function() {
											$("#file").hide();
											document.getElementById("file").removeAttribute("required");
										});
										$("#delete-file").click(function(){
											$("#file").show();
											$("#file-box").hide();
											document.getElementById("file").setAttribute("required", "required");
										});
										</script>
									<?php endif;?>
                                    <input name="file" id="file" class="form-control" type="file" required="required"/><br>
                                </div>
                            </div>
                        </div>
                        <input type="submit" value="Submit" class="btn btn-lg btn-success btn-block"/>
                        <?= form_close() ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
	<script>
	    var getData;
		
		function setRequest(){
			if (window.XMLHttpRequest){
				return new XMLHttpRequest();
			}
			if (window.ActiveXObject){
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			return null;
		}
		function ShowParentKategori(cat_id){
			getData = setRequest();
			var url= "<?=site_url()?>/surat/get_kategori/"+cat_id;
			console.log("Parentkategori:"+url);
			getData.open("GET",url,true);
			getData.onreadystatechange=stateChangedParentKategori;
			getData.send(null);
		}	
		function stateChangedParentKategori(){
			var data='';
			var insert;
			if (getData.readyState==4){
				insert= JSON.parse(getData.responseText);
				data += '<option value="-1">Pilih</option>';
				$.each (insert.kategori, function (index, eachdata){
					data += '<option value="'+eachdata.id+'">'+eachdata.nama+'</option>';
				});
				console.log("dataparsing:"+data);
				document.getElementById("dinas").innerHTML = data;
				$("#kategori").val('-1');
			}
		}
		function showKategori(cat_id){
			getData = setRequest();
			var url= "<?=site_url()?>/surat/get_kategori/"+cat_id;
			console.log("kategori:"+url);
			getData.open("GET",url,true);
			getData.onreadystatechange= stateChangedKategori;
			getData.send(null);
		}	
		function stateChangedKategori(){
			var data='';
			var insert;
			if (getData.readyState==4){
				insert= JSON.parse(getData.responseText);
				data += '<option value="-1">Pilih</option>';
				$.each (insert.kategori, function (index, eachdata){
					data += '<option value="'+eachdata.id+'">'+eachdata.nama+'</option>';
				});
				console.log("dataparsing:"+data);
				document.getElementById("kategori").innerHTML = data;
			}
		} 
	</script>
 <?php $this->load->view('headerfooter/footer'); ?>
